package com.combetter.changxing;

import android.app.Application;
import android.content.Context;
import android.support.multidex.MultiDex;
import android.text.TextUtils;

import com.clj.fastble.BleManager;
import com.combetter.changxing.db.AppDatabase;
import com.combetter.changxing.db.entity.Facility;
import com.combetter.changxing.db.entity.User;
import com.combetter.changxing.util.LogUtil;
import com.combetter.changxing.util.Utils;
import com.umeng.commonsdk.UMConfigure;
import com.umeng.socialize.PlatformConfig;
import com.uuzuche.lib_zxing.activity.ZXingLibrary;

import java.util.List;

import cn.jpush.android.api.JPushInterface;

/**
 * Created by mark.fan on 2018/5/3.
 */

public class MyApp extends Application {

    private AppExecutors mExecutors;

    private static MyApp sInstance;

    @Override
    public void onCreate() {
        super.onCreate();
        LogUtil.isDebug = true;
        sInstance = this;
        Utils.init(getApplicationContext());
        mExecutors = new AppExecutors();

        JPushInterface.setDebugMode(false);
        JPushInterface.init(this);

        Thread.setDefaultUncaughtExceptionHandler((t, e) -> e.printStackTrace());

        //二维码初始化
        ZXingLibrary.initDisplayOpinion(this);
        //蓝牙初始化
        BleManager.getInstance().init(this);
        BleManager.getInstance()
                .enableLog(false)
                .setReConnectCount(10, 5000)
                .setConnectOverTime(5000)
                .setOperateTimeout(5000);

        UMConfigure.init(this, "5c35d017b465f5acc3000923"
                , "umeng", UMConfigure.DEVICE_TYPE_PHONE, "");//58edcfeb310c93091c000be2 5965ee00734be40b580001a0


        getDatabase();
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }

    //各个平台的配置，建议放在全局Application或者程序入口
    {
//        PlatformConfig.setWeixin("wx748ba5a7ccd55926", "8d31c4290df723b7cb7f6b328028f542");
        PlatformConfig.setWeixin("wx6184ad302aef52f4", "20d098bda66d95536d605dba16583376");
        PlatformConfig.setQQZone("1108127886", "XSlJAd1TYIHSv4p1");
    }

    public static MyApp getInstance() {
        return sInstance;
    }

    public AppExecutors getExecutors() {
        return mExecutors;
    }

    public AppDatabase getDatabase() {
        return AppDatabase.getInstance(this, mExecutors);
    }

    public User getUserInfo() {
        return getDatabase().userDao().getUserInfo();
    }

    public List<Facility> getFacilityInfo() {
        if (getUserInfo() != null && !TextUtils.isEmpty(getUserInfo().getUid())) {
            return getDatabase().facilityDao().findByUid(getUserInfo().getUid());
        } else {
            return null;
        }
    }

    public void resetFacilityInfo() {
//        List<Facility> facility = getDatabase().facilityDao().getFacilityInfoList();
//        if (facility != null) {
//            getDatabase().facilityDao().insertAllOrSingle(facility);
//        }
    }

    public void resetUserInfo() {
        try {
            User user = getDatabase().userDao().getUserInfo();
            if (user != null) {
                user.setFacilityVersion("");
                user.setPreheatingTime("");
                user.setPreheatingTemperature("");
                user.setHeatingTime("");
                user.setHeatingTemperature("");
                user.setTemperatureTrimming("0℃");
                user.setTimeTrimming("0s");
                user.setElectricQuantity(0);
            }
            if (getDatabase().userDao() != null) {
                getDatabase().userDao().insertUser(user);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
